/**
* require PreferenceStringContainer.js
*/ 

function AttackStringContainer() {
	this.construct();
}

AttackStringContainer.prototype = {
	defaultAttack	:	Array(Array("@@\"><script>alert(1);</script>", "\"><script>alert(1);</script>", "false"),
							  Array("@@<body onload=prompt(1)>", "<body onload=prompt(1)>", "false"),
							  Array("+/v9 +ADwAaAB0AG0APgA8AGIAbwBkAHkAPgA8AHMAYwByAGkAcAB0AD4AYQBsAGUAcgB0ACgAMQApADsAPAAvAHMAYwByAGkAcAB0AD4APAAvAGIAbwBkAHkAPgA8AC8AaAB0AG0APg-", "^\\+/v9 \\+ADwAaAB0AG0APgA8AGIAbwBkAHkAPgA8AHMAYwByAGkAcAB0AD4AYQBsAGUAcgB0ACgAMQApADsAPAAvAHMAYwByAGkAcAB0AD4APAAvAGIAbwBkAHkAPgA8AC8AaAB0AG0APg-", "true")), 
	attackStrings	:	Array(),
	
	construct:	function(){
		//如果preference中的attack String为空，存入一个默认值
		//@TODO 需要加一个监听事件，如果preference被修改（用户主动修改），要执行construct，否则attackStrings和preference里的会对不上
		this.getAttacks();
		if(this.attackStrings == "") {
			this.attackStrings = this.defaultAttack;
			preferenceStringContainer.setStrings(this.attackStrings, "attacks");
		}
    },
	
	reset: function(mywindow){
		this.attackStrings = this.defaultAttack;
		if(confirm("确认重置为默认项")){
			preferenceStringContainer.setStrings(this.attackStrings, "attacks");
			this.flush(mywindow);
		}
		mywindow.focus();
		return true;
	},
	
	getAttacks:	function() {
		this.attackStrings = preferenceStringContainer.getStrings("attacks");
	},

	addAttack:	function(attackString, validateString, isRegex, mywindow){
		if(!attackString){
            return false;
        }
		
		var regex = isRegex?"true":"false";
        
		this.attackStrings.push(Array(attackString, validateString, regex));
		preferenceStringContainer.setStrings(this.attackStrings, "attacks");
		this.flush(mywindow);
		return true;
	},
	
	removeAttack:	function(mywindow){
		var tree = mywindow.document.getElementById("xssTree");
		this.attackStrings.splice(tree.currentIndex,1);
		preferenceStringContainer.setStrings(this.attackStrings, "attacks");
		this.flush(mywindow);
	},
	
	checkUniq:	function(string){
		for(var i=0;i<this.attackStrings.length;i++){
			if(this.attackStrings[i] == string)
				return false;
		}
		return true;
	},

	importAttacks:	function(mywindow){
	    var nsIFilePicker = Components.interfaces.nsIFilePicker;
        var picker = Components.classes["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
        picker.init(window, "Select File To Import From", nsIFilePicker.modeOpen);
        picker.appendFilter("All Files", '*');
        var resultOfPicker = picker.show();
        if (resultOfPicker == nsIFilePicker.returnCancel){
            return false;
        }
        var importFile = picker.file;
		var istream = Components.classes["@mozilla.org/network/file-input-stream;1"]
                                .createInstance(Components.interfaces.nsIFileInputStream);
        var sstream = Components.classes["@mozilla.org/scriptableinputstream;1"]
					  .createInstance(Components.interfaces.nsIScriptableInputStream);
		istream.init(importFile, 0x01, 0444, 0);
		sstream.init(istream);
        
		//有没有readline的...
		data = "";
		str = sstream.read(4096);
        while (str.length > 0) {
          data += str;
          str = sstream.read(4096);
        }

        istream.close();
		sstream.close();
		
		lines = data.split("\r\n"); 
		for(i=0; i<lines.length; i++){
			var arr = lines[i].split("\t");
			var attackStr = arr[0];
			var validateStr = arr[1];
			var isRegex = arr[2];
			this.attackStrings.push(Array(attackStr, validateStr, isRegex));
		}
		preferenceStringContainer.setStrings(this.attackStrings, "attacks");
		this.flush(mywindow);
	},
	
	exportAttacks:	function(mywindow){
		var nsIFilePicker = Components.interfaces.nsIFilePicker;
		var picker = Components.classes['@mozilla.org/filepicker;1'].createInstance(nsIFilePicker);		
        picker.init(mywindow, "Select File To Export To", nsIFilePicker.modeSave);
        picker.appendFilter("All Files", '*');
		
		var resultOfPicker = picker.show();
        if (resultOfPicker == nsIFilePicker.returnCancel){
            return false;
        }
        var exportFile = picker.file;

        var foStream = Components.classes["@mozilla.org/network/file-output-stream;1"]
                .createInstance(Components.interfaces.nsIFileOutputStream);
		
		foStream.init(exportFile, 0x02 | 0x08 | 0x20, 0666, 0); // write, create, truncate
		
		for(i=0; i<this.attackStrings.length-1; i++){
			var line = this.attackStrings[i][0]+"\t"+this.attackStrings[i][1]+"\t"+this.attackStrings[i][2]+"\r\n";
			foStream.write(line, line.length);
		}
		//最后一行不加换行
		var line = this.attackStrings[i][0]+"\t"+this.attackStrings[i][1]+"\t"+this.attackStrings[i][2];
		foStream.write(line, line.length);
        foStream.close();
    },


	flush:	function(mywindow){
		this.getAttacks();
	
		var treechildren = mywindow.document.getElementById("existingXSSstrings");

		while(treechildren.childNodes.length>0){		
			treechildren.removeChild(treechildren.childNodes[0]);
		}

		for(var i=0;i<this.attackStrings.length;i++){
			var treeitem = mywindow.document.createElement("treeitem");
			
			var row = mywindow.document.createElement("treerow");
			
			var cell = mywindow.document.createElement('treecell');
			cell.setAttribute('label', this.attackStrings[i][0]);
			row.appendChild(cell);
			
			cell = document.createElement('treecell');
			cell.setAttribute('label', this.attackStrings[i][1]);
			row.appendChild(cell);
			
			cell = document.createElement('treecell');
			if(this.attackStrings[i][2] == "false")
				label = "否";
			else if(this.attackStrings[i][2] == "true") 
				label = "是";
			cell.setAttribute('label', label);
			row.appendChild(cell);
			
			treeitem.appendChild(row);
			treechildren.appendChild(treeitem);
		}
	}
};
